﻿<Window x:Class="Grouping"
    xmlns:local="clr-namespace:DataBinding"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Grouping" Height="228" Width="332">
    <Window.Resources>
        <CollectionViewSource Source="{x:Static local:Data.People}" x:Key="cvs">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Profession" />
                <scm:SortDescription PropertyName="LastName" />
            </CollectionViewSource.SortDescriptions>
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Profession"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource cvs}}">
            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Background="LightGreen" FontWeight="Bold" FontSize="15"
                         Text="{Binding Path=Name}" x:Name="tbGroupTitle" />
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=Name}" Value="Enemy Agent">
                                    <Setter TargetName="tbGroupTitle" Property="Background" Value="Red" />
                                    <Setter TargetName="tbGroupTitle" Property="Foreground" Value="Yellow" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=Name}" Value="Citizen">
                                    <Setter TargetName="tbGroupTitle" Property="Background" Value="LightGray" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListBox.GroupStyle>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,7" Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=FirstName}" FontSize="16" />
                        <TextBlock Text="{Binding Path=LastName}"  FontSize="16" Margin="3.5,0,7,0" />
                        <TextBlock Text="{Binding Path=Birthday, StringFormat=d}" FontSize="16" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

